package com.olda.play.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.olda.play.constant.Constant;
import com.olda.play.enums.ResponseCode;
import com.olda.play.model.DropOption;
import com.olda.play.rpc.CommonResponseDto;
import com.olda.play.system.biz.SysDeptBizService;
import com.olda.play.system.biz.dao.SysDeptBizMapper;
import com.olda.play.system.dto.SysDeptDto;
import com.olda.play.system.model.po.SysDept;
import com.olda.play.system.service.SysDeptService;
import com.olda.play.system.util.SysUtils;
import com.olda.play.system.vo.SysDeptVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门业务接口
 *  列表不分页查询、新增部门、删除部门、编辑部门
 *
 * @author oldA
 *
 */
@Service
@RequestMapping(value =  "/dept")
@RestController
public class SysDeptServiceImpl implements SysDeptService {

    @Autowired
    private SysDeptBizMapper sysDeptBizMapper;

    @Autowired
    private SysDeptBizService sysDeptBizService;

    /**
     * 部门列表查询
     * @return
     */
    @Override
    @RequestMapping(value = "/querySysDeptList",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<List<SysDeptDto>> querySysDeptList(@RequestBody SysDeptVo param) {
        List<SysDeptDto> list=sysDeptBizService.querySysDeptList(param);
        return CommonResponseDto.success(SysUtils.generateRecurDepts(list));
    }


    /**
     * 新增或修改部门
     * @param param
     * @return
     */
    @Override
    @RequestMapping(value = "/addOrUpdateDept",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public CommonResponseDto<Boolean> saveDept(@RequestBody SysDeptVo param) {
        if(param==null){
            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        Long id=param.getId();
        if(id==null){
            //新增-- 注意判断业务字段编码是否重复
            SysDept sysDeptOld=sysDeptBizService.selectSysDeptByName(param.getName());
            if(sysDeptOld!=null){
                return CommonResponseDto.error(ResponseCode.MORE_DEPT);
            }
            SysDept sysDept=BeanUtil.copyProperties(param,SysDept.class);
            sysDeptBizService.save(sysDept);
        }else{
//            SysDept old=sysDeptBizService.selectSysDeptById(id);
            SysDept sysDept=BeanUtil.copyProperties(param,SysDept.class);
            sysDeptBizService.updateSysDept(sysDept);
        }
        return CommonResponseDto.success(true);
    }


    /**
     * 删除部门
     * @param id 部门ID
     * @return
     */
    @Override
    @RequestMapping(value = "/deleteSysDeptById",method = RequestMethod.GET ,produces="application/json;charset=UTF-8" )
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public CommonResponseDto<Boolean> deleteByIds(@RequestParam Long id) {
        if(id==null){
            return CommonResponseDto.error(ResponseCode.ILLEGAL_ARGUMENT);
        }
        SysDept sysDept=sysDeptBizService.selectSysDeptById(id);
        if(sysDept==null){
            return CommonResponseDto.error(ResponseCode.HAS_NO_DATA);
        }
        sysDeptBizService.deleteSyDeptById(id);
        return CommonResponseDto.success(true);
    }



    /**
     * 新增部门时获取下拉部门树
     * @return
     */
    @Override
    @RequestMapping(value = "/lisetDeptOptions",method = RequestMethod.POST ,produces="application/json;charset=UTF-8" )
    public CommonResponseDto<List<DropOption>> lisetDeptOptions() {
        SysDeptVo param=new SysDeptVo();
        param.setStatus(1);
        List<SysDeptDto> list=sysDeptBizService.querySysDeptList(param);
        return CommonResponseDto.success(SysUtils.recurDropDepts(Constant.ROOT_DEPT_ID,list));
    }




}
